package com.itheima.web.MyFilter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter("/*")
public class EncodingFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) servletRequest;
        HttpServletResponse resp = (HttpServletResponse) servletResponse;
        //请求编码格式
        req.setCharacterEncoding("UTF-8");
        //响应编码格式
        resp.setContentType("text/html;charSet=UTF-8");

        //放行
        filterChain.doFilter(req,resp);
    }
}
